(function($){
	$(document).ready(function(){
		$("#id_app").css('display', 'none');
		$("#lookup_id_app").after("&nbsp;&nbsp;&nbsp;<a target='_blank' href='##' id='id_app_name'></a>");
		
		$("#id_cross_local").bind('input', function(){
			if($(this).val() != ""){
				var local = $(this).val().replace(/\\/g, '/');
				if(local.indexOf(":") > -1){
					var prefix = local.split(":", 1);
					$("#id_cross_remote").val(local.replace(prefix + ":", 'https://www.dropbox.com/home/Small Tree Media/Project Submission Doc'));
				}else{
					var prefix = local.split("/Project Submission Doc/", 1);
					$("#id_cross_remote").val(local.replace(prefix, 'https://www.dropbox.com/home/Small Tree Media'));
				}
			}else{
				$("#id_cross_remote").val("");
			}
		});
		
		$("#id_app_name").parent().append("<span class='help-block'><strong><i>Click the 'magnifying glass' icon to choose an app</i></strong></span>");
		
		$("div.control-group.form-row.field-cross_promo_ads ").css('display', 'none');
		$("div.control-group.form-row.field-cross_local ").css('display', 'none');
		$("div.control-group.form-row.field-cross_remote ").css('display', 'none');
		$("div.control-group.form-row.field-get_cross_promo_ads ").css('display', 'none');
		
		
		if($("#id_cross_promo option[value='Y']").attr('selected') == true){
			$("div.control-group.form-row.field-cross_promo_ads ").css('display', 'block');
			$("div.control-group.form-row.field-cross_local ").css('display', 'block');
			$("div.control-group.form-row.field-cross_remote ").css('display', 'block');
		}
		
		if($("div.control-group.form-row.field-cross_promo div div.controls span").html() == 'Yes'){
			$("div.control-group.form-row.field-cross_promo_ads ").css('display', 'block');
			$("div.control-group.form-row.field-cross_local ").css('display', 'block');
			$("div.control-group.form-row.field-cross_remote ").css('display', 'block');
			$("div.control-group.form-row.field-get_cross_promo_ads ").css('display', 'block');
		}
		
		$("#id_cross_promo").change(function(e){
			console.log(e.target.value);
			if(e.target.value == 'Y'){
				$("input[type='checkbox']").attr('checked', true);
				$("input[type='checkbox']").bind('click', function(e){return false});
				$("div.control-group.form-row.field-cross_promo_ads ").css('display', 'block');
				$("div.control-group.form-row.field-cross_local ").css('display', 'block');
				$("div.control-group.form-row.field-cross_remote ").css('display', 'block');
			}else{
				$("input[type='checkbox']").attr('checked', false);
				$("div.control-group.form-row.field-cross_promo_ads ").css('display', 'none');
				$("div.control-group.form-row.field-cross_local ").css('display', 'none');
				$("div.control-group.form-row.field-cross_remote ").css('display', 'none');
			}
		});
		
		// $.each($("#result_list tbody tr"), function(){
			// if($(this).find('td').eq(2).html() == 'Invalid'){
				// $(this).find('td').eq(2).html("<font color='red'>Invalid</font>");
			// }
		// });
	});
})(django.jQuery);
